package CAS;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicStampedReference;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @program: javase
 * @description: CAS比较并替换
 * @author: lld
 * @create: 2021-03-26 20:19
 **/
public class CAS {
    private static AtomicInteger race = new AtomicInteger(0);
//  private static volatile int race=0;
    private static void increase(){
        race.getAndIncrement();

    }
    public static void main(String[] args) throws InterruptedException {

        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                for (int i1 = 0; i1 < 1000; i1++) {
                    increase();
                }
//                countDownLatch.countDown();
            }).start();
        }
//        countDownLatch.await();
//        sleep保证主线程在后面输出
        Thread.sleep(2000);
        System.out.println(race);

//        CAS存在ABA问题
//        解决方法：1.使用原子标记引用类中的compareAndSet
        AtomicStampedReference reference = new AtomicStampedReference(null,1);
//        reference.compareAndSet();

        AtomicReference atomicReference = new AtomicReference();

//        ReentrantLock

    }
}
